package cn.edu.lingnan.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class WebHandlerConfig implements WebMvcConfigurer {

    @Override
   // 这个方法用来注册拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptorConfig())
                .addPathPatterns("/**")
                .excludePathPatterns("/")
                .excludePathPatterns("/static/**","/css/**")
                .excludePathPatterns("/user/getNumCode")
                .excludePathPatterns("/user/userLogin")
                .excludePathPatterns("/swagger-ui")
                .excludePathPatterns("/uploadImg/**")
                //.excludePathPatterns("/swagger-resources/**", "/swagger-ui.html/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/swagger-ui.html/**");
                //.excludePathPatterns("/webjars/**");

    }


    ////这个方法用来注册拦截器
    //@Override
    //public void addInterceptors(InterceptorRegistry registry) {
    //    registry.addInterceptor(new HandlerInterceptorConfig())
    //            //拦截所有请求
    //            .addPathPatterns("/**")
    //            //放行
    //            .excludePathPatterns("/bookpage/login-3")
    //            .excludePathPatterns("/static/**", "/css/**")
    //            .excludePathPatterns("/user/getNumCode")
    //            .excludePathPatterns("/")
    //            .excludePathPatterns("/user/login")
    //            .excludePathPatterns("/swagger-ui")
    //            .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/swagger-ui.html/**");
    //
    //}




    /**
     * 配置swagger2的静态资源路径
     * 如果继承了WebMvcConfigurationSupport，
     *          则在配置文件在中配置的相关内容会失效，需要重新指定静态资源
     *
     * 需要重新指定swagger静态资源
     */
    // 这个方法是用来配置静态资源的，比如html，js，css，等等
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问 【静态资源路径】
        registry.addResourceHandler("/resources/**") //表示文件路径
                .addResourceLocations("classpath:/resources/static/"); //表示要放开的资源
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //  图片访问路径
        registry.addResourceHandler("/uploadImg/**")
                .addResourceLocations("classpath:/uploadImg/");


        //registry.addResourceHandler("/**")
        //        .addResourceLocations("classpath:/resources/")
        //        .addResourceLocations("classpath:/static/")
        //        .addResourceLocations("classpath:/public/");


    }

    public void addViewControllers(ViewControllerRegistry registry){
        //映射主页面
        registry.addViewController("/index").setViewName("/index");
        registry.addViewController("/addWelcome").setViewName("/bookpage/welcome-1");
        registry.addViewController("/").setViewName("/bookpage/login-3");
        registry.addViewController("/imgs/addImage").setViewName("/imgs/addImage");
        registry.addViewController("/imgs/imgHtml").setViewName("/imgs/images.html");

    }



}
